<?php

class Part extends Controller {

	function __construct()
	{
		parent::Controller();
		if (!$this->site_sentry->is_logged_in()) {redirect('login/');exit;}
		$this->load->model('vendors_model', false, false, 'vendors');
		$this->load->model('parts_model', false, false, 'part');


	}
	function index()
	{
		if ( $post = $this->input->post('search_part_number') )
			{
			
				
				$postInfo = array(
							'part_number'		=> $this->input->post('search_part_number'),
							'part_type'		=> $this->input->post('search_part_type_id'),
							'description'		=> $this->input->post('search_part_description')
						);

				$data['query'] = $this->parts_model->getSearchResults($postInfo);
			}
			else
			{
				$offset = (int) $this->uri->segment(3, 0);
				$data['query'] = $this->parts_model->getParts($offset, 5000);
			}
		$data['partList'] = $this->parts_model->getAllParts(); // activate the option
		$data['extraHeadContent'] = "<script type=\"text/javascript\" src=\"". base_url()."js/newinvoice.js\"></script>\n";
		$data['extraHeadContent'] .= "<script type=\"text/javascript\" src=\"". base_url()."js/search.js\"></script>\n";
		$data['extraHeadContent'] .= "<link type=\"text/css\" rel=\"stylesheet\" href=\"". base_url()."css/invoice.css\" />\n";
		
		$data['total_rows'] = ($data['query']) ? $data['query']->num_rows() : 0;
		$data['vendorList'] = $this->vendors_model->getAllVendors(); // activate the option
		$data['uomList'] = $this->parts_model->getUOM(); // activate the option
		$data['typeList'] = $this->parts_model->getPartType(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');
		$data['msg'] = '<p class="error">' . $this->session->flashdata('message') . "</p>\n";
		$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('index', false, false, 'part');
		$this->load->view('login_footer', false , false, 'common_assets');
	}

	function delete()
	{
		$id = (int) $this->uri->segment(3);
		$this->session->set_flashdata('deletePart', $id);
		$partInfo = $this->parts_model->getSinglePart($id);
		$data['deletePart'] = $partInfo->row();
		$data['page_title'] = $this->lang->line('menu_delete_part');
	
		$this->load->view('delete', $data, false, 'part');
		
	}

	function delete_confirmed() {
		$part_id = $this->session->flashdata('deletePart');
		$this->parts_model->deletePart($part_id);
		$this->session->set_flashdata('deletePart', $this->lang->line('part_part_delete_success'));
		redirect('part/');
		exit;
	}

	function show()
	{
		$id = (int) $this->uri->segment(3);
		$partInfo = $this->parts_model->getSinglePart($id);		
		$data['row'] = $partInfo->row();
		$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/form.css\" type=\"text/css\" \n />";
		$data['typeList'] = $this->parts_model->getPartType(); // activate the option
		$data['uomList'] = $this->parts_model->getUOM(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');



		$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('show', false, false, 'part');
		$this->load->view('login_footer', false , false, 'common_assets');

	}

	function edit()
	{
		$id = (int) $this->uri->segment(3);
		$partInfo = $this->parts_model->getSinglePart($id);		
		$data['row'] = $partInfo->row();
		$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/form.css\" type=\"text/css\" \n />";
			//$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/theme.css\" type=\"text/css\" \n />";
		$data['typeList'] = $this->parts_model->getPartType(); // activate the option
		$data['uomList'] = $this->parts_model->getUOM(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');


			if ( $post = $this->input->post('part_id') )
			{
				$data = array(
							'part_number'		=> $this->input->post('part_number'),
							'part_type'		=> $this->input->post('part_type_id'),
							'description'		=> $this->input->post('part_description'),
							'comment'		=> $this->input->post('part_comment'),
							'value'			=> $this->input->post('part_value'),
							'unit_of_measure'	=> $this->input->post('unit_of_measure'),
						);
					
				$this->db->where('id', $this->input->post('part_id'));
				$this->db->update('parts', $data);
				
				$this->session->set_flashdata('notification', 'Part "'.$this->input->post("part_number").'" has been updated ...');
				
				redirect('part/edit/'.$this->input->post('part_id'));
				//redirect('part');
			}
					$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('edit'.$this->input->post('id'), false, false, 'part');
			$this->load->view('login_footer', false , false, 'common_assets');
		

	}


	function create()
	{

			if ( $post = $this->input->post('part_number') )
			{
				$data = array(
							'part_number'		=> $this->input->post('part_number'),
							'part_type'		=> $this->input->post('part_type_id'),
							'description'		=> $this->input->post('part_description'),
							'comment'		=> $this->input->post('part_comment'),
							'value'			=> $this->input->post('part_value'),
							'unit_of_measure'	=> $this->input->post('unit_of_measure'),
						);
						
				$this->parts_model->addPart($data);
				$id = $this->db->insert_id();	
					
				$this->session->set_flashdata('notification', 'Part "'.$this->input->post("part_number").'" has been created');	
				
				redirect('part/index');
			}
			else
			{
		$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/form.css\" type=\"text/css\" \n />";
		//$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/theme.css\" type=\"text/css\" \n />";
		$data['typeList'] = $this->parts_model->getPartType(); // activate the option
		$data['uomList'] = $this->parts_model->getUOM(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');
		$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('new', false, false, 'part');
		$this->load->view('login_footer', false , false, 'common_assets');
			}
	}



}
?>
